<?php

/**
 * Zeed Platform Project
 * Based on Zeed Framework & Zend Framework.
 * 
 * LICENSE
 * http://www.zeed.com.cn/license/
 * 
 * @category   Zeed
 * @package    Zeed_ChangeMe
 * @subpackage ChangeMe
 * @copyright  Copyright (c) 2010 Zeed Technologies PRC Inc. (http://www.zeed.com.cn)
 * @author     Zeed Team (http://blog.zeed.com.cn)
 * @since      2011-3-21
 * @version    SVN: $Id$
 */
class SearchController extends TrendAdminAbstract
{
    public $perpage = 20;
    
    /**
     * 全局搜索
     */
    public function index ()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        $this->addResult(self::RS_SUCCESS, 'php', 'search.index');
        return parent::multipleResult(self::RS_SUCCESS);
    }
    
    /**
     * 搜索 - 案件
     */
    public function getArchiveContents ()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $page = (int) $this->input->get('pageIndex', 0);
        $perpage = $this->input->get('pageSize', $this->perpage);
        $key = trim($this->input->get('key'));
        
        /* ajax 加载数据 */
        if ($this->input->isAJAX()) {
            $offset = $page * $perpage;
            
            /* 组织查询条件 */
            $where = array('status' => array('$in' => array(-2, 1)));
            if ($key) {
                $where['$and'][] = array(
                        '$or' => array(
                                array('sn' => new MongoRegex("/$key/i")),
                                array('title' => new MongoRegex("/$key/i")),
                                array('category_name' => new MongoRegex("/$key/i")),
                                array('department_name' => new MongoRegex("/$key/i")),
                                array('description' => new MongoRegex("/$key/i")),
                                array('label' => new MongoRegex("/$key/i")),
                        )
                );
            }
            $order = array('ctime' => 'DESC');
            
            $content = Archive_Model_Content::instance()->fetchByWhere($where, $order, $perpage, $offset);
            $data['count'] = Archive_Model_Content::instance()->getCount($where);
            
            /* 处理其他一些信息 */
            if (! empty($content)) {
                foreach ($content as $k => &$v) {
                    /* 处理标题 */
                    if (mb_strlen($v['title']) > 16) {
                        $v['title'] = mb_substr($v['title'], 0, 16) . '...';
                    }
                    
                    /* 处理时间 */
                    if (strlen($v['ctime']) == 13) {
                        $v['ctime'] = date(DATETIME_FORMAT, $v['ctime'] / 1000);
                    } else {
                        $v['ctime'] = date(DATETIME_FORMAT, $v['ctime']);
                    }
                    if (strlen($v['mtime']) == 13) {
                        $v['mtime'] = date(DATETIME_FORMAT, $v['mtime'] / 1000);
                    } else {
                        $v['mtime'] = date(DATETIME_FORMAT, $v['mtime']);
                    }
                    
                    /* 处理行动次数 */
                    $where_card = array('sn' => $v['sn'], 'status' => 1);
                    $v['count_card'] = Archive_Model_Card::instance()->getCount($where_card);
                }
            }
            
            $data['content'] = $content ? $content : array();
        }
        
        $this->setData('data', $data);
        return parent::multipleResult(self::RS_SUCCESS);
    }
    
    /**
     * 搜索 - 行动
     */
    public function getCardContents ()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $page = (int) $this->input->get('pageIndex', 0);
        $perpage = $this->input->get('pageSize', $this->perpage);
        $key = trim($this->input->get('key'));
        
        /* ajax 加载数据 */
        if ($this->input->isAJAX()) {
            $offset = $page * $perpage;
            
            /* 组织查询条件 */
            $where = array('status' => array('$in' => array(-2, 1)));
            if ($key) {
                $where['$and'][] = array(
                        '$or' => array(
                                array('sn' => new MongoRegex("/$key/i")),
                                array('title' => new MongoRegex("/$key/i")),
                                array('respondent' => new MongoRegex("/$key/i")),
                                array('address' => new MongoRegex("/$key/i")),
                                array('description' => new MongoRegex("/$key/i")),
                                array('label' => new MongoRegex("/$key/i")),
                        )
                );
            }
            $order = array('ctime' => 'DESC');
            
            $content = Archive_Model_Card::instance()->fetchByWhere($where, $order, $perpage, $offset);
            $data['count'] = Archive_Model_Card::instance()->getCount($where);
            
            /* 处理其他一些信息 */
            if (! empty($content)) {
                foreach ($content as $k => &$v) {
                    /* 处理标题 */
                    if (mb_strlen($v['title']) > 16) {
                        $v['title'] = mb_substr($v['title'], 0, 16) . '...';
                    }
                    
                    /* 处理时间 */
                    if (strlen($v['ctime']) == 13) {
                        $v['ctime'] = date(DATETIME_FORMAT, $v['ctime'] / 1000);
                    } else {
                        $v['ctime'] = date(DATETIME_FORMAT, $v['ctime']);
                    }
                    
                    /* 处理所属案件 */
                    $archive_info = Archive_Model_Content::instance()->fetchByFV('sn', $v['sn']);
                    if (! empty($archive_info)) {
                        $v['title_archive'] = $archive_info[0]['title'];
                        if (mb_strlen($archive_info[0]['title']) > 16) {
                            $v['title_archive'] = mb_substr($archive_info[0]['title'], 0, 16) . '...';
                        }
                        $v['archive_id'] = $archive_info[0]['_id'];
                        $v['status'] = $archive_info[0]['status'];
                    } else {
                        $v['title_archive'] = '';
                        $v['archive_id'] = '';
                    }
                }
            }
            
            $data['content'] = $content ? $content : array();
        }
        
        $this->setData('data', $data);
        return parent::multipleResult(self::RS_SUCCESS);
    }
    
    /**
     * 搜索 - 录像
     */
    public function getVideoContents ()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $page = (int) $this->input->get('pageIndex', 0);
        $perpage = $this->input->get('pageSize', $this->perpage);
        $key = trim($this->input->get('key'));
        
        /* ajax 加载数据 */
        if ($this->input->isAJAX()) {
            $offset = $page * $perpage;
            
            /* 组织查询条件 */
            $where = array(
                    'mediatype' => 'video',
                    'pid' => '',
                    'status' => array('$in' => array(-4, 1))
            );
            if ($key) {
                $where['$and'][] = array(
                        '$or' => array(
                                array('title' => new MongoRegex("/$key/i")),
                        )
                );
            }
            $order = array('ctime' => 'DESC');
            
            $content = Attachment_Model_Content::instance()->fetchByWhere($where, $order, $perpage, $offset);
            $data['count'] = Attachment_Model_Content::instance()->getCount($where);
            
            /* 处理其他一些信息 */
            if (! empty($content)) {
                foreach ($content as $k => &$v) {
                    /* 处理标题 */
                    if (mb_strlen($v['title']) > 16) {
                        $v['title'] = mb_substr($v['title'], 0, 16) . '...';
                    }
                    
                    /* 查询隶属资料卡 */
                    $v['belongto_card'] = '';
                    $where_card = array('attachments_video' => new MongoRegex("/" . $v['_id'] . "/i"));
                    $card_info = Archive_Model_Card::instance()->fetchByWhere($where_card);
                    if (! empty($card_info)) {
                        $v['belongto_card'] = $card_info[0]['title'];
                        if (mb_strlen($card_info[0]['title']) > 16) {
                            $v['belongto_card'] = mb_substr($card_info[0]['title'], 0, 16) . '...';
                        }
                        if ($card_info[0]['status'] == -2) {
                            $v['status'] = -4;
                        }
                    }
                    
                    /* 查询隶属案件 */
                    $v['belongto_archive'] = '';
                    if (! empty($card_info)) {
                        $archive_info = Archive_Model_Content::instance()->fetchByFV('sn', $card_info[0]['sn']);
                        if (! empty($archive_info)) {
                            $v['belongto_archive'] = $archive_info[0]['title'];
                            if (mb_strlen($archive_info[0]['title']) > 16) {
                                $v['belongto_archive'] = mb_substr($archive_info[0]['title'], 0, 16) . '...';
                            }
                            if ($archive_info[0]['status'] == -2) {
                                $v['status'] = -4;
                            }
                        }
                    }
                    
                    /* 处理时间 */
                    if (strlen($v['ctime']) == 13) {
                        $v['ctime'] = date(DATETIME_FORMAT, $v['ctime'] / 1000);
                    } else {
                        $v['ctime'] = date(DATETIME_FORMAT, $v['ctime']);
                    }
                    
                    /* 处理地址 */
                    $v['filepath'] = Support_Image_Url::getImageUrl($v['filepath']);
                }
            }
            
            $data['content'] = $content ? $content : array();
        }
        
        $this->setData('data', $data);
        return parent::multipleResult(self::RS_SUCCESS);
    }
    
    /**
     * 搜索 - 照片
     */
    public function getImageContents ()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $page = (int) $this->input->get('pageIndex', 0);
        $perpage = $this->input->get('pageSize', $this->perpage);
        $key = trim($this->input->get('key'));
        
        /* ajax 加载数据 */
        if ($this->input->isAJAX()) {
            $offset = $page * $perpage;
            
            /* 组织查询条件 */
            $where = array(
                    'mediatype' => 'image',
                    'pid' => '',
                    'status' => array('$in' => array(-4, 1))
            );
            if ($key) {
                $where['$and'][] = array(
                        '$or' => array(
                                array('title' => new MongoRegex("/$key/i")),
                        )
                );
            }
            $order = array('ctime' => 'DESC');
            
            $content = Attachment_Model_Content::instance()->fetchByWhere($where, $order, $perpage, $offset);
            $data['count'] = Attachment_Model_Content::instance()->getCount($where);
            
            /* 处理其他一些信息 */
            if (! empty($content)) {
                foreach ($content as $k => &$v) {
                    /* 处理标题 */
                    if (mb_strlen($v['title']) > 16) {
                        $v['title'] = mb_substr($v['title'], 0, 16) . '...';
                    }
                    
                    /* 查询隶属资料卡 */
                    $v['belongto_card'] = '';
                    $where_card = array('attachments_img' => new MongoRegex("/" . $v['_id'] . "/i"));
                    $card_info = Archive_Model_Card::instance()->fetchByWhere($where_card);
                    if (! empty($card_info)) {
                        $v['belongto_card'] = $card_info[0]['title'];
                        if (mb_strlen($card_info[0]['title']) > 16) {
                            $v['belongto_card'] = mb_substr($card_info[0]['title'], 0, 16) . '...';
                        }
                        if ($card_info[0]['status'] == -2) {
                            $v['status'] = -4;
                        }
                    }
                    
                    /* 查询隶属案件 */
                    $v['belongto_archive'] = '';
                    if (! empty($card_info)) {
                        $archive_info = Archive_Model_Content::instance()->fetchByFV('sn', $card_info[0]['sn']);
                        if (! empty($archive_info)) {
                            $v['belongto_archive'] = $archive_info[0]['title'];
                            if (mb_strlen($archive_info[0]['title']) > 16) {
                                $v['belongto_archive'] = mb_substr($archive_info[0]['title'], 0, 16) . '...';
                            }
                            if ($archive_info[0]['status'] == -2) {
                                $v['status'] = -4;
                            }
                        }
                    }
                    
                    /* 处理时间 */
                    if (strlen($v['ctime']) == 13) {
                        $v['ctime'] = date(DATETIME_FORMAT, $v['ctime'] / 1000);
                    } else {
                        $v['ctime'] = date(DATETIME_FORMAT, $v['ctime']);
                    }
                    
                    /* 处理地址 */
                    $v['filepath'] = Support_Image_Url::getImageUrl($v['filepath']);
                }
            }
            
            $data['content'] = $content ? $content : array();
        }
        
        $this->setData('data', $data);
        return parent::multipleResult(self::RS_SUCCESS);
    }
    
    /**
     * 搜索 - 录音
     */
    public function getAudioContents ()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $page = (int) $this->input->get('pageIndex', 0);
        $perpage = $this->input->get('pageSize', $this->perpage);
        $key = trim($this->input->get('key'));
        
        /* ajax 加载数据 */
        if ($this->input->isAJAX()) {
            $offset = $page * $perpage;
            
            /* 组织查询条件 */
            $where = array(
                    'mediatype' => 'audio',
                    'pid' => '',
                    'status' => array('$in' => array(-4, 1))
            );
            if ($key) {
                $where['$and'][] = array(
                        '$or' => array(
                                array('title' => new MongoRegex("/$key/i")),
                        )
                );
            }
            $order = array('ctime' => 'DESC');
            
            $content = Attachment_Model_Content::instance()->fetchByWhere($where, $order, $perpage, $offset);
            $data['count'] = Attachment_Model_Content::instance()->getCount($where);
            
            /* 处理其他一些信息 */
            if (! empty($content)) {
                foreach ($content as $k => &$v) {
                    /* 处理标题 */
                    if (mb_strlen($v['title']) > 16) {
                        $v['title'] = mb_substr($v['title'], 0, 16) . '...';
                    }
                    
                    /* 查询隶属资料卡 */
                    $v['belongto_card'] = '';
                    $where_card = array('attachments_audio' => new MongoRegex("/" . $v['_id'] . "/i"));
                    $card_info = Archive_Model_Card::instance()->fetchByWhere($where_card);
                    if (! empty($card_info)) {
                        $v['belongto_card'] = $card_info[0]['title'];
                        if (mb_strlen($card_info[0]['title']) > 16) {
                            $v['belongto_card'] = mb_substr($card_info[0]['title'], 0, 16) . '...';
                        }
                        if ($card_info[0]['status'] == -2) {
                            $v['status'] = -4;
                        }
                    }
                    
                    /* 查询隶属案件 */
                    $v['belongto_archive'] = '';
                    if (! empty($card_info)) {
                        $archive_info = Archive_Model_Content::instance()->fetchByFV('sn', $card_info[0]['sn']);
                        if (! empty($archive_info)) {
                            $v['belongto_archive'] = $archive_info[0]['title'];
                            if (mb_strlen($archive_info[0]['title']) > 16) {
                                $v['belongto_archive'] = mb_substr($archive_info[0]['title'], 0, 16) . '...';
                            }
                            if ($archive_info[0]['status'] == -2) {
                                $v['status'] = -4;
                            }
                        }
                    }
                    
                    /* 处理时间 */
                    if (strlen($v['ctime']) == 13) {
                        $v['ctime'] = date(DATETIME_FORMAT, $v['ctime'] / 1000);
                    } else {
                        $v['ctime'] = date(DATETIME_FORMAT, $v['ctime']);
                    }
                    
                    /* 处理地址 */
                    $v['filepath'] = Support_Image_Url::getImageUrl($v['filepath']);
                }
            }
            
            $data['content'] = $content ? $content : array();
        }
        
        $this->setData('data', $data);
        return parent::multipleResult(self::RS_SUCCESS);
    }
    
    /**
     * 搜索 - 打点
     */
    public function getDotContents ()
    {
        $this->addResult(self::RS_SUCCESS, 'json');
        
        /* 接收参数 */
        $page = (int) $this->input->get('pageIndex', 0);
        $perpage = $this->input->get('pageSize', $this->perpage);
        $key = trim($this->input->get('key'));

        /* 对关键字做特殊处理 */
        $key = str_replace('*', '\*', $key);
        
        /* ajax 加载数据 */
        if ($this->input->isAJAX()) {
            $offset = $page * $perpage;
            
            /* 组织查询条件 */
            $where = array();
            if ($key) {
                $where['$and'][] = array(
                        '$or' => array(
                                array('content' => new MongoRegex("/$key/i")),
                        )
                );
            }
            $order = array('ctime' => 'DESC');
            
            $content = Attachment_Model_Content_Dot::instance()->fetchByWhere($where, $order, $perpage, $offset);
            $data['count'] = Attachment_Model_Content_Dot::instance()->getCount($where);
            
            /* 处理其他一些信息 */
            if (! empty($content)) {
                foreach ($content as $k => &$v) {
                    /* 处理标题 */
                    if (mb_strlen($v['content']) > 16) {
                        $v['content'] = mb_substr($v['content'], 0, 16) . '...';
                    }
                    /* 查询隶属文件信息 */
                    $v['belongto_attachment'] = '';
                    $attachment_info = Attachment_Model_Content::instance()->fetchByPK($v['attachment_id']);
                    if (! empty($attachment_info)) {
                        $attachment_title = $attachment_info[0]['title'];
                        if (mb_strlen($attachment_info[0]['title']) > 16) {
                            $attachment_title = mb_substr($attachment_info[0]['title'], 0, 16) . '...';
                        }
                        $v['belongto_attachment'] = $attachment_title . '.' . $attachment_info[0]['suffix'];
                        if ($attachment_info[0]['status'] == -4) {
                            $v['status'] = -4;
                        }
                    }
                    
                    /* 查询隶属资料卡 */
                    $v['belongto_card'] = '';
                    $card_info = Archive_Model_Card::instance()->fetchByPK($v['card_id']);
                    if (! empty($card_info)) {
                        $v['belongto_card'] = $card_info[0]['title'];
                        if (mb_strlen($card_info[0]['title']) > 16) {
                            $v['belongto_card'] = mb_substr($card_info[0]['title'], 0, 16) . '...';
                        }
                        if ($card_info[0]['status'] == -2) {
                            $v['status'] = -2;
                        }
                    }
                    
                    /* 查询隶属案件 */
                    $v['belongto_archive'] = '';
                    $archive_info = Archive_Model_Content::instance()->fetchByFV('sn', $v['sn']);
                    if (! empty($archive_info)) {
                        $v['belongto_archive'] = $archive_info[0]['title'];
                        if (mb_strlen($archive_info[0]['title']) > 16) {
                            $v['belongto_archive'] = mb_substr($archive_info[0]['title'], 0, 16) . '...';
                        }
                        if ($archive_info[0]['status'] == -2) {
                            $v['status'] = -22;
                        }
                    }
                    
                    /* 处理时间 */
                    if (strlen($v['ctime']) == 13) {
                        $v['ctime'] = date(DATETIME_FORMAT, $v['ctime'] / 1000);
                    } else {
                        $v['ctime'] = date(DATETIME_FORMAT, $v['ctime']);
                    }
                }
            }
            
            $data['content'] = $content ? $content : array();
        }
        
        $this->setData('data', $data);
        return parent::multipleResult(self::RS_SUCCESS);
    }
}

// End ^ Native EOL ^ UTF-8